<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .demo {
        font-family: sans-serif;
        border: 1px solid #eee;
        border-radius: 2px;
        padding: 20px 30px;
        margin-top: 1em;
        margin-bottom: 40px;
        user-select: none;
        overflow-x: auto;
      }
    </style>
  </head>
  <body>
    <div id="app"></div>

    <script src="../lib/vue3.js"></script>
    <script>
      const app = Vue.createApp({
        template: `
          <div class="demo">
            <Hello ref="myHello" /> 

            <hr />

            <button @click="$refs.myHello.increment()">自增</button>
          </div>
        `,

        mounted() {
          console.log(this.$refs.myHello); // Hello 组件的实例对象

          // 下面代码没有问题，但是一般不建议直接操作子组件的数据，而是通过调用子组件提供的方法来做
          // this.$refs.myHello.count++;

          // this.$refs.myHello.increment();
        },
      });

      app.component("Hello", {
        template: `
          <div>
            Hello, {{ count }}
          </div>
        `,

        data() {
          return {
            count: 1,
          };
        },

        methods: {
          increment() {
            this.count++;
          },
        },
      });

      app.mount("#app");
    </script>
  </body>
</html>
